using System;
using System.Reflection;
using HelloWorld.Helper;

namespace HelloWorld.Learn
{
    public class AttributeDemo
    {
        public static void Demo()
        {
            Type widgetType = typeof(Widget);

            // 获取类的自定义属性
            var widgetClassAttribute = widgetType.GetCustomAttributes(typeof(HelpAttribute), false);
            if (widgetClassAttribute.Length > 0)
            {
                HelpAttribute attr = (HelpAttribute)widgetClassAttribute[0];
                Console.WriteLine($"Widget class help URL : {attr.Url} - related topic : {attr.Topic}");
            }
            
            // 获取类的方法
            var displayMethod = widgetType.GetMethod(nameof(Widget.Display));
            // 获取方法的自定义属性
            var displayMethodAttribute = displayMethod.GetCustomAttributes(typeof(HelpAttribute), false);
            if (displayMethodAttribute.Length > 0)
            {
                HelpAttribute attr = (HelpAttribute)displayMethodAttribute[0];
                Console.WriteLine($"Display method help URL : {attr.Url} - related topic : {attr.Topic}");
            }

            Console.WriteLine();
        }
    }
}